package com.whq.thrift.api.web.admin.service.baseconf.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.whq.thrift.api.common.spring.datasource.DataSourceContextHolder;
import com.whq.thrift.api.web.admin.db.baseconf.ModuleInstance;
import com.whq.thrift.api.web.admin.db.baseconf.ModuleInstanceMapper;
import com.whq.thrift.api.web.admin.service.baseconf.ModuleInstanceService;

@Service
public class ModuleInstanceServiceImpl implements ModuleInstanceService{
	
	@Autowired
	private ModuleInstanceMapper moduleInstanceMapper;
	
	public void addRecord(ModuleInstance record) {
		DataSourceContextHolder.set("dataSourceBaseServiceConfig");
		if (record == null) {
			return ;
		}
		Date now  = new Date();
		record.setCreateTime(now);
		record.setUpdateTime(now);
		
		this.moduleInstanceMapper.addRecord(record);
	}
	
	public List<ModuleInstance> findRecordsByMap(Map<String, Object> whereParams, int beginIndex, int maxCount) {
		DataSourceContextHolder.set("dataSourceBaseServiceConfig");
		
		if (whereParams == null) {
			whereParams = new HashMap<String, Object>(0);
		}
		whereParams.put("beginIndex", beginIndex);
		whereParams.put("maxCount", maxCount);
		return this.moduleInstanceMapper.findRecordsByMap(whereParams);
	}
	
	public void updateRecord(ModuleInstance record) {
		DataSourceContextHolder.set("dataSourceBaseServiceConfig");
		if (record == null) {
			return ;
		}
		Date now  = new Date();
		record.setUpdateTime(now);
		
		this.moduleInstanceMapper.updRecord(record);
	}
	
	public void deleteRecord(String id) {
		DataSourceContextHolder.set("dataSourceBaseServiceConfig");
		
		this.moduleInstanceMapper.delRecord(id);
	}

	public int findRecordCount(Map<String, Object> whereParams) {
		DataSourceContextHolder.set("dataSourceBaseServiceConfig");
		
		return this.moduleInstanceMapper.findRecordsCount(whereParams);
	}
	
	public ModuleInstance findRecordById(String id) {
		DataSourceContextHolder.set("dataSourceBaseServiceConfig");
		
		return this.moduleInstanceMapper.getRecordByKey(id);
	}

}
